package com.demo.server.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.server.config.mqtt.MqttUtil;
import com.demo.server.constant.CommonConstant;
import com.demo.server.entity.Device;
import com.demo.server.entity.dto.DeviceDto;
import com.demo.server.entity.req.CommonSearchReq;
import com.demo.server.entity.req.DeviceAddReq;
import com.demo.server.entity.req.DeviceSwitchReq;
import com.demo.server.mapper.DeviceMapper;
import com.demo.server.util.AccessTokenUtil;
import com.demo.server.util.Result;
import com.demo.server.util.ResultPage;
import com.demo.server.util.UUIDUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class DeviceService {
	
public static final String DEVICE_EXIST  = "设备已存在";
	
	public static final String DEVICE_ON= "1"; //设备开
	public static final String DEVICE_OFF = "0";//设备关
	
	@Autowired
	DeviceMapper deviceMapper;
	
	@Autowired(required = false)
	private MqttUtil mqttUtil;
	
	/**
	 * @title 查询设备
	 * @param req
	 * @return
	 */
	public ResultPage<Device> search(CommonSearchReq req) {
		Page<Object> page = PageHelper.startPage(req.getPage(), req.getSize());
		DeviceDto dto = new DeviceDto();
		BeanUtils.copyProperties(req, dto);
		List<Device> devices = deviceMapper.select(dto);
		return ResultPage.success(devices, page.getTotal());
	}
	
	/**
	 * @title 添加设备
	 * @param record
	 * @return
	 */
	public Result add(DeviceAddReq req) {
		// TODO Auto-generated method stub
		String userId = AccessTokenUtil.getUserId();
		Device existD = deviceMapper.selectByPrimaryKey(req.getMac());
		if(existD != null) {
			return Result.fail(DEVICE_EXIST);
		}
		Device device = new Device();
		BeanUtils.copyProperties(req,device);
		Date now = new Date();
		if(StringUtils.isEmpty(device.getMac())) {
			device.setMac(UUIDUtil.getDateRandom(10000));
		}
		device.setOpen(DEVICE_OFF);
		device.setCreatetime(now);
		device.setModifytime(now);
		device.setTenantId(userId);
		deviceMapper.insertSelective(device);
		return Result.success();
	}
	
	/**
	 * @title 修改设备
	 * @param record
	 * @return Result
	 */
	public Result edit(DeviceAddReq req) {
		// TODO Auto-generated method stub
		Device device = new Device();
		BeanUtils.copyProperties(req,device);
		device.setModifytime(new Date());
		deviceMapper.updateByPrimaryKeySelective(device);
		return Result.success();
	}
	
	/**
	 * @title 删除设备 
	 * @param mac
	 * @return
	 */
	public Result delete(String mac) {
		// TODO Auto-generated method stub
		deviceMapper.deleteByPrimaryKey(mac);
		return Result.success(); 
	}
	
	
	/**
	 * @title 开关设备
	 * @param req
	 * @return Result
	 */
	public Result switchDevice(DeviceSwitchReq req) {
		// TODO Auto-generated method stub
		Device device = new Device();
		device.setMac(req.getMac());
		device.setOpen(req.getOpen());
		deviceMapper.updateByPrimaryKeySelective(device);
		if(mqttUtil != null){
			mqttUtil.pub(CommonConstant.MQTT_PUBLISH_PREFIX+req.getMac(), req.getOpen(),CommonConstant.MQTT_QOS0);
		}
		return Result.success(); 
	}
	
}
